package book;
import constant.Constant;
import util.AnalyzingBook;

import java.io.IOException;

public class Library {
    private Book[] books;//当前图书数组
    private int bookCount;//实际存储的图书数量(记录当前图书馆有效的书是多少本)
    private  final AnalyzingBook analyzingBook=new AnalyzingBook(); //书籍解析相关类
    public Library()
    {
        loadAllBook();
    }

    private void loadAllBook() {
        try {
            //读取文件内容
            Book[] AllBook=analyzingBook.loadObject(Constant.ALL_BOOK_FILE_NAME);
            books=new Book[Constant.CAPACITY];
            //是否有数据 没有数据 有效书籍个数为0
            if (AllBook==null){
                bookCount=0;
            }
            else {
                //查看实际书籍长度是多少 是否大于默认长度5
                int allBookLen=AllBook.length;
                //实际书籍长度大于默认分配的长度，books数组需要将大小改为实际长度
                if (allBookLen>books.length){
                    books=new Book[allBookLen];
                }
                for (int i = 0; i <AllBook.length; i++) {
                    books[i]=AllBook[i];     //把读到的元素一一存到books数组中
                }
                bookCount=allBookLen;//修改实际有效书籍个数
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //将图书存储到文件中
    private void  storeBook(){
        try {
            analyzingBook.storeObject(books, Constant.ALL_BOOK_FILE_NAME);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public void searchBook() {
    }

    public void displayBooks() {
    }

    public void exit() {
    }

    public void addBook(Book book) {
    }

    public int getBookId() {
    }

    public Book searchBookById(int bookId) {
    }

    public void borrowBook(int bookId) {
    }
}
